import { 
     Cartesian3,
     Quaternion
 } from 'cesium'
export const projectOnPlane = (vp:any, vn:any) => {
    const vt = new Cartesian3()
    const multi = new Cartesian3()
    const divide = new Cartesian3()
    const cartesian3 = Cartesian3.multiplyByScalar(vn, Cartesian3.dot(vp, vn), multi);
    Cartesian3.divideByScalar(
        cartesian3,
        Cartesian3.dot(vn, vn),
        divide
    )
    Cartesian3.subtract(vp, divide, vt)
    return vt
}

export const rayPlaneIntersection = (ray:any, cameraDirection:any, pickPoint:any, result = new Cartesian3()) => {
    if (!pickPoint) {
        throw new Error("cuowu")
        return
    }
    const number = Cartesian3.dot(cameraDirection, pickPoint);
    const number1 = Cartesian3.dot(cameraDirection, ray.origin);
    const number2 = Cartesian3.dot(cameraDirection, ray.direction);
    const t = (number - number1) / number2
    return Cartesian3.add(ray.origin, Cartesian3.multiplyByScalar(ray.direction, t, result), result)
}
export const cartesian3ToQuaternion = (cartesian3:Cartesian3) => {
    return new Quaternion(
        cartesian3.x,
        cartesian3.y,
        cartesian3.z,
        0
    )
}

function moduloQuaternion(quaternion:any) {
    let result = Math.sqrt(quaternion.x*quaternion.x + quaternion.y*quaternion.y + quaternion.z*quaternion.z+ quaternion.w*quaternion.w)
    console.log(result)
    return result
}

export const normalizingQuaternion = (quaternion:any) => {
    // Normalize( q ) = q/ |q| = q / (x*x + y*y + z*z + w*w)
    return Quaternion.divideByScalar(
        quaternion,
        moduloQuaternion(quaternion),
        quaternion
    )
}
